<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.titleKeyword" placeholder="标题" clearable style="width: 130px;" class="filter-item" />
      <el-select v-model="listQuery.category" placeholder="分类" clearable style="width: 140px; margin-left: 10px;" class="filter-item">
        <el-option v-for="item in categoryOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-select v-model="listQuery.status" placeholder="状态" clearable style="width: 140px; margin-left: 10px;" class="filter-item">
        <el-option v-for="item in statusOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-refresh" @click="handleRefresh">
        重置
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-circle-plus-outline" @click="handleAdd">
        添加
      </el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" :header-cell-style="headerCellStyle" :cell-style="{padding:'0px'}" border stripe element-loading-text="拼命加载中">
      <el-table-column label="分类" align="center" width="110px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.category | categoryFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="110px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.status | statusFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" min-width="110px">
        <template slot-scope="{row}">
          <router-link :to="'/manager/info/help/'+row.id+'/edit'">
            <el-link type="primary" style="float: left;">{{ row.title }}</el-link>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" width="160px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.publishedTime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog title="帮助添加" :visible.sync="addVisible">
      <el-form ref="addForm" :rules="addRules" :model="add" label-width="70px" style="margin-left:0px;">
        <el-form-item label="分类" prop="category">
          <el-select v-model="add.category" clearable>
            <el-option v-for="item in categoryOptions" :key="item.key" :label="item.display_name" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="add.status" clearable>
            <el-option v-for="item in statusOptions" :key="item.key" :label="item.display_name" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="add.title" clearable style="width: 400px;" />
        </el-form-item>
        <el-form-item label="正文" prop="content">
          <el-input v-model="add.content" type="textarea" :rows="4" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="发布时间" prop="publishedTime">
          <el-date-picker
            v-model="add.publishedTime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder=""
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取消</el-button>
        <el-button type="primary" @click="addConfirm()">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { getHelpByPage, addHelp } from '@/api/manager/info-api'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { toOptionsList, toKeyValueMap } from '@/utils/index'

var categoryKeyValue = {}
var statusKeyValue = {}

export default {
  name: 'ManagerHelp',
  components: { Pagination },
  directives: { waves },
  filters: {
    categoryFilter(category) {
      return categoryKeyValue[category]
    },
    statusFilter(status) {
      return statusKeyValue[status]
    }
  },
  data() {
    return {
      categoryOptions: [],
      statusOptions: [],
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        titleKeyword: null,
        category: null,
        status: null
      },

      listLoading: true,
      list: null,
      total: 0,

      addVisible: false,
      add: {
        category: null,
        status: null,
        title: null,
        content: null,
        publishedTime: null
      },
      addRules: {
        category: [{
          required: true, message: '请输入分类', trigger: 'blur'
        }],
        status: [{
          required: true, message: '请输入状态', trigger: 'blur'
        }],
        title: [{
          required: true, message: '请输入标题', trigger: 'blur'
        }]
      }

    }
  },
  async created() {
    await this.initLoad()
    await this.getList()
  },
  methods: {
    async initLoad() {
      const dataDictsParam = ['HELP_CENTER_HELP_CATEGORY', 'HELP_CENTER_HELP_STATUS']
      const { data } = await fetchEnum(dataDictsParam)
      for (const dataDict of data) {
        if (dataDict.code === 'HELP_CENTER_HELP_CATEGORY') {
          if (dataDict.options) {
            this.categoryOptions = toOptionsList(dataDict.options, 'code', 'name')
            categoryKeyValue = toKeyValueMap(this.categoryOptions, 'key', 'display_name')
          }
        }
        if (dataDict.code === 'HELP_CENTER_HELP_STATUS') {
          if (dataDict.options) {
            this.statusOptions = toOptionsList(dataDict.options, 'code', 'name')
            statusKeyValue = toKeyValueMap(this.statusOptions, 'key', 'display_name')
          }
        }
      }
      this.initListQuery()
    },
    getList() {
      this.listLoading = true
      getHelpByPage(this.listQuery).then(response => {
        const { data } = response
        this.list = data.result
        this.total = data.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.listLoading = false
      })
    },
    initListQuery() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 20,
        category: null,
        status: null,
        titleKeyword: null
      }
    },

    handleFilter() {
      this.getList()
    },
    handleRefresh() {
      this.initListQuery()
      this.getList()
    },
    handleAdd() {
      this.add = {
        category: null,
        status: null,
        title: null,
        content: null,
        publishedTime: null
      }
      this.addVisible = true
    },
    headerCellStyle({ row, rowIndex }) {
      return 'background-color: #42b983; color: #FFF;'
    },

    addConfirm() {
      this.$refs.addForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.add))
        addHelp(data).then(response => {
          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
          this.addVisible = false
          this.getList()
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    }

  }
}

</script>
